/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-11-14 下午1:31 Created by dwzhang
 */
package com.jade.modules.protocol.impl;

import java.util.Date;
import java.util.List;
import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Order;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.modules.protocol.*;
import org.springframework.transaction.annotation.Transactional;

/**
 * 协议管理器实现。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-11-14 下午1:31
 */
//@Named ("module_protocolManager")
public class ProtocolManagerImpl
        implements ProtocolManager
{
    private ProtocolDao protocolDao;

//    @Inject
    public void setProtocolDao(@Named ("module_protocolDao") ProtocolDao protocolDao)
    {
        this.protocolDao = protocolDao;
    }

    protected void checkName(ProtocolChannel channel)
        throws ProtocolException
    {
        Criteria criteria = new Criteria(0, 1);
        criteria.add(Restrictions.eq("name", channel.getName()));
        List<ProtocolChannel> channels = protocolDao.listChannel(channel.getType(), criteria);
        if (!channels.isEmpty()&&channels.get(0).getId()!=channel.getId()) {
            throw new ProtocolException("err.module.protocol.channel.name_conflict", "Duplicated channel name", null,
                    channel.getName());
        }

    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void addChannel(ProtocolChannel channel)
            throws ProtocolException
    {
        checkName(channel);
        Date now = new Date();
        channel.setCreationDate(now);
        channel.setUpdateDate(now);
        protocolDao.addChannel(channel);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void updateChannel(ProtocolChannel channel)
            throws ProtocolException
    {
        checkName(channel);
        channel.setUpdateDate(new Date());
        protocolDao.updateChannel(channel);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void removeChannel(int... ids)
            throws ProtocolException
    {
        for (int id : ids) {
            ProtocolChannel channel = getChannel(id);
            if (channel != null)
                protocolDao.removeChannel(channel);
        }
    }

    public ProtocolChannel getChannel(int id)
    {
        return protocolDao.getChannel(id);
    }

    public PagingList<ProtocolChannel> queryChannel(String type, Criteria criteria)
    {
        return protocolDao.queryChannel(type, criteria);
    }

    public List<ProtocolChannel> listChannel(String type, Criteria criteria)
    {
        return protocolDao.listChannel(type, criteria);
    }

    public ProtocolChannel selectChannel(String type)
    {
        Criteria criteria = new Criteria(0,1);
        criteria.add(Restrictions.eq("status", ProtocolChannel.STATUS_ACTIVE));
        criteria.addOrder(Order.desc("sortCode"));
        List<ProtocolChannel> channels = listChannel(type, criteria);
        return channels.isEmpty() ? null : channels.get(0);
    }

    public List<ProtocolParameter> getParameters(String type, String protocol)
    {
        return protocolDao.getParameters(type, protocol);
    }
}
